﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.Threading;
using System.Data.SqlClient;
using System.Data.Common;
using Apq_DBTools.Forms;
using Apq.TreeListView;
using System.IO;
using org.mozilla.intl.chardet;
using Apq.DllImports;
using System.Collections;
using MySql.Data.MySqlClient;
using System.Text.RegularExpressions;
using System.Reflection;
using System.Diagnostics;

namespace Apq_DBTools.Forms
{
	public partial class Help : Apq.Windows.Forms.DockForm
	{
		public Help()
		{
			InitializeComponent();
		}

		public override void SetUILang(Apq.UILang.UILang UILang)
		{
			Text = Apq.GlobalObject.UILang["帮助"];

			tsbGoBack.Text = Apq.GlobalObject.UILang["后退"];
			tsbGoForward.Text = Apq.GlobalObject.UILang["前进"];
		}

		private string uriIndex = string.Empty;

		private void SqlGen_Load(object sender, EventArgs e)
		{
			// 初始页面
			string strIndex = Path.GetDirectoryName(Apq.GlobalObject.TheProcess.MainModule.FileName) + @"\Help\index.htm";
			strIndex = strIndex.Replace("\\", "/");
			tscbUrl.Text = strIndex;
			tsbRefresh_Click(sender, e);
			Uri url = new Uri(strIndex);
			uriIndex = url.AbsoluteUri;
		}

		#region UI事件响应
		private void tsbGoForward_Click(object sender, EventArgs e)
		{
			webBrowser1.GoForward();
		}

		private void tsbGoBack_Click(object sender, EventArgs e)
		{
			webBrowser1.GoBack();
		}

		private void tsbRefresh_Click(object sender, EventArgs e)
		{
			webBrowser1.Navigate(tscbUrl.Text);

			if (!tscbUrl.Items.Contains(tscbUrl.Text))
			{
				tscbUrl.Items.Add(tscbUrl.Text);
			}
		}
		#endregion

		#region 浏览器控件事件响应
		private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
		{
			/* 解析原始串并使用反射执行相应动作
			 * 
			 * 格式：op:opname(params)
			 * 
			 * method:方法(参数列表)		调用静态方法(常用)，类名固定为此窗口类，参数类型字符串
			 * MainForm:方法(参数列表)	调用主窗口的方法
			 * form:窗口类名(程序集名)	从主界面打开新窗口
			 * 
			 * */
			if (e.Url.OriginalString.StartsWith("method:", StringComparison.OrdinalIgnoreCase))
			{
				e.Cancel = true;

				int idxPBegin = e.Url.OriginalString.IndexOf("(", 7);
				int idxPEnd = e.Url.OriginalString.IndexOf(")", idxPBegin + 1);

				// 类名固定为此窗口类

				// 方法名
				string MName = e.Url.OriginalString.Substring(7, idxPBegin - 7);

				// 解析参数
				string[] Params = e.Url.OriginalString.Substring(idxPBegin + 1, idxPEnd - idxPBegin - 1).Split(',');

				MethodInfo mi = this.GetType().GetMethod(MName);
				if (mi.IsStatic)
				{
					mi.Invoke(null, Params);
				}
			}
			if (e.Url.OriginalString.StartsWith("MainForm:", StringComparison.OrdinalIgnoreCase))
			{
				e.Cancel = true;

				int idxPBegin = e.Url.OriginalString.IndexOf("(", 9);
				int idxPEnd = e.Url.OriginalString.IndexOf(")", idxPBegin + 1);

				// 类名固定为此窗口类

				// 方法名
				string MName = e.Url.OriginalString.Substring(9, idxPBegin - 9);

				// 解析参数
				string[] Params = e.Url.OriginalString.Substring(idxPBegin + 1, idxPEnd - idxPBegin - 1).Split(',');
				if (Params.Length > 1 && "e".Equals(Params[1], StringComparison.OrdinalIgnoreCase))
				{
					Params[1] = null;
				}

				MethodInfo mi = GlobalObject.MainForm.GetType().GetMethod(MName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				if (!mi.IsStatic)
				{
					mi.Invoke(GlobalObject.MainForm, Params);
				}
			}
			if (e.Url.OriginalString.StartsWith("form:", StringComparison.OrdinalIgnoreCase))
			{
				e.Cancel = true;

				int idxPBegin = e.Url.OriginalString.IndexOf("(", 5);
				int idxPEnd = e.Url.OriginalString.IndexOf(")", idxPBegin + 1);

				// 类名
				string FormClass = e.Url.OriginalString.Substring(5, idxPBegin - 6);

				// 程序集名
				string AsmName = e.Url.OriginalString.Substring(idxPBegin + 1, idxPEnd - idxPBegin - 1);

				Assembly asm = System.Reflection.Assembly.LoadFrom(AsmName);
				if (asm != null)
				{
					Apq.Windows.Forms.DockForm form = asm.CreateInstance(FormClass) as Apq.Windows.Forms.DockForm;
					if (form != null)
					{
						form.Show(GlobalObject.MainForm.dockPanel1);
					}
				}
			}
		}

		private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
		{
			if (uriIndex.Equals(e.Url.AbsoluteUri, StringComparison.OrdinalIgnoreCase))
			{
				return;
			}

			HtmlElement hBody = null;
			foreach (HtmlWindow window in webBrowser1.Document.Window.Frames)
			{
				if (window.Url.Equals(e.Url))
				{
					hBody = window.Document.Body;
					break;
				}
			}
			if (hBody == null || string.IsNullOrWhiteSpace(hBody.InnerHtml))
			{
				return;
			}

			/* 查找"$...$"并替换为相应语言的文字
			 * 
			 * */
			Regex reg = new Regex(@"\$(.*?)\$");

			string strBodyHtml = hBody.InnerHtml;
			MatchCollection mc = reg.Matches(strBodyHtml);
			foreach (Match m in mc)
			{
				if (m.Groups.Count > 0)
				{
					string strUILang = Apq.GlobalObject.UILang[m.Groups[1].Value];

					strBodyHtml = strBodyHtml.Replace(m.Captures[0].Value, strUILang);
				}
			}

			if (!strBodyHtml.Equals(hBody.InnerHtml))
			{
				hBody.InnerHtml = strBodyHtml;
			}
		}
		#endregion

		#region 页面调用的静态方法
		#endregion
	}
}